package com.wooming.studentinfosystem.service.impl;

import com.wooming.studentinfosystem.dto.StudentInfoDTO;
import com.wooming.studentinfosystem.entity.*;
import com.wooming.studentinfosystem.mapper.StudentAddMapper;
import com.wooming.studentinfosystem.service.interf.StudentAdd;
import com.wooming.studentinfosystem.util.MapAndJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 学生信息添加服务实现类
 *
 * @author Wooming
 */
@Service
public class StudentAddService implements StudentAdd {

    @Autowired
    private StudentAddMapper studentAddMapper;

    @Override
    public void addStudent(Student student) {
        // 构建动态SQL参数
        Map<String, Object> params = new HashMap<>();
        params.put("studentId", student.getStudentId());
        params.put("name", student.getName());
        params.put("age", student.getAge());
        params.put("gender", student.getGender());
        params.put("classNum", student.getClassNum());
        params.put("type", student.getType());
        params.put("province", student.getAddress().getProvince());
        params.put("city", student.getAddress().getCity());
        params.put("street", student.getAddress().getStreet());
        params.put("doorNumber", student.getAddress().getDoorNumber());
        if (student instanceof Graduate) {
            params.put("major", null);
            params.put("supervisor", ((Graduate) student).getSupervisor());
            params.put("researchField", ((Graduate) student).getResearchField());
        } else if (student instanceof Undergraduate) {
            params.put("major", ((Undergraduate) student).getMajor());
            params.put("supervisor", null);
            params.put("researchField", null);
        }
        params.put("scores", MapAndJson.mapToJson(student.getScores()));

        //测试用代码:Map对象是否成功转换为JSON字符串
//        System.out.println(params.get("scores"));

        // 调用mapper方法
        studentAddMapper.addStudent(params);
    }

    /**
     * 将StudentInfoDTO对象转换为Student实体对象
     * @param studentInfoDTO StudentInfoDTO对象
     * @return 实体对象
     */
    @Override
    public Student convertToEntity(StudentInfoDTO studentInfoDTO) {
        Student student = null;
        if (studentInfoDTO.getType().equals(StudentType.GRADUATE.toString())) {
            student = new Graduate(studentInfoDTO.getSupervisor(), studentInfoDTO.getResearchFiled());
        } else if (studentInfoDTO.getType().equals(StudentType.UNDERGRAD.toString())) {
            student = new Undergraduate(studentInfoDTO.getMajor());
        }
        if (student != null) {
            student.setStudentId(studentInfoDTO.getStudentId());
            student.setName(studentInfoDTO.getName());
            student.setAge(studentInfoDTO.getAge());
            student.setGender(Gender.valueOf(studentInfoDTO.getGender()));
            student.setClassNum(studentInfoDTO.getClassNum());
            student.setType(StudentType.valueOf(studentInfoDTO.getType()));
            student.setAddress(new Address(
                    studentInfoDTO.getProvince(),
                    studentInfoDTO.getCity(),
                    studentInfoDTO.getStreet(),
                    studentInfoDTO.getDoorNumber()
            ));
            student.setScores(studentInfoDTO.getScores());
        }
        return student;
    }

    /**
     * 检查学生ID是否存在
     * @param studentId 学生ID
     * @return 是否存在
     */
    @Override
    public boolean isStudentIdExists(String studentId) {
        return studentAddMapper.countByStudentId(studentId) > 0;
    }
}
